package com.bgs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgs.mapper.LoginMapper;
import com.bgs.pojo.RegisterInfo;
import com.bgs.pojo.User;
import com.bgs.pojo.UserUpdatePassword;
import com.bgs.pojo.UserVo;
import com.bgs.service.LoginService;
import com.bgs.tool.GuliException;
import com.bgs.tool.RedisUtil;
import com.bgs.utils.TokenUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

/**
 * @program: xkgc
 * @description:
 * @author: Mr.Zhao
 * @create: 2022-05-09 15:24
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper,User> implements LoginService {

    Logger log= LogManager.getLogger();

    @Resource
    private LoginMapper loginMapper;

    @Autowired
    private RedisUtil redisUtil;


    @Override
    public UserVo login(UserVo s) {
        User user = getUser(s);

        if (user!=null){
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            String token = TokenUtil.getToken(user.getEmail(),user.getPassword());
            userVo.setToken(token);
            return userVo;
        }else {
            throw new GuliException(20001,"用户名或密码错误");
        }
    }

    @Override
    public User getEmail(String email) {
           User u=loginMapper.selectUser(email);
        return u;
    }

    @Override
    public Integer getEmails(String emails)
    {
        return loginMapper.getEmails(emails);
    }

    @Override
    public void uup(UserUpdatePassword upp) {


        loginMapper.uup(upp);

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void registerInfo(RegisterInfo registerInfo) {

       try{
           registerInfo.setIsDel("n");
           loginMapper.addStore(registerInfo);
           loginMapper.addUser(registerInfo);
           loginMapper.addStoreimg(registerInfo);
           loginMapper.addStoreimgunion(registerInfo);
           loginMapper.addSeniority(registerInfo);

       }catch (Exception e){
           log.error(e);
           TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
       }





    }

    private User getUser(UserVo s) {

        String email = s.getEmail();
        String password = s.getPassword();

//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//
//        wrapper.eq("phone",phone);
//        wrapper.eq("password",password);

        User ee=null;
        try {
           // ee = baseMapper.selectOne(wrapper)
            ee=loginMapper.selectAll(email,password);
        }catch (Exception e){
            e.printStackTrace();
            try {
                throw new GuliException(20001,"系统错误");
            }catch (Exception e1){
                e.printStackTrace();
            }
        }

       return ee;

    }


}
